package com.swss.g.sys.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.swss.g.base.impl.BaseServiceImpl;
import com.swss.g.sys.bean.CurrentPeriod;
import com.swss.g.sys.bean.Danjuno;
import com.swss.g.sys.bean.GFunc;
import com.swss.g.sys.dao.CurrentPeriodDAO;
import com.swss.g.sys.dao.DanjunoDAO;
import com.swss.g.sys.dao.GFuncDAO;
import com.swss.g.sys.service.CurrentPeriodService;
import com.swss.util.DoDate;

public class CurrentPeriodServiceImpl extends BaseServiceImpl<CurrentPeriod>
		implements CurrentPeriodService {
	static Logger log = Logger.getLogger(CurrentPeriodServiceImpl.class);
	private CurrentPeriodDAO currentPeriodDao;
	private GFuncDAO gfuncDao;
	private DanjunoDAO danjunoDao;

	/**
	 * 补完最大编号
	 * 
	 * @param no
	 * @return
	 * @throws Exception
	 */
	public String filler(Integer no, Integer maxLength) throws Exception {
		String str = "";

		int len = no.toString().length();

		for (int i = 0; i < (maxLength - len); i++) {
			str += "0";
		}
		return str + no;
	}

	public String[] getAllNo(String year, int count, int maxNo) {
		String[] nos = new String[count];
		try {
			for (int i = 0; i < count; i++) {
				nos[i] = year + filler(maxNo + i + 1, 4);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return nos;
	}

	public String[] getNo(char type, boolean isCreateAccount, String currYear,
			int count) throws Exception {
		// TODO Auto-generated method stub
		// 是否为建账阶段
		if (!isCreateAccount) {
			// 维护账阶段 使用系统时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			currYear = sdf.format(new Date());
		}
		CurrentPeriod findcp = new CurrentPeriod();
		findcp.setYearNo(currYear);
		CurrentPeriod cp = this.currentPeriodDao.findOneByExample(findcp);
		// 是否存在这一期
		if (cp == null) {
			// 不存在 创建
			this.createCurr(currYear);
			cp = this.currentPeriodDao.findOneByExample(findcp);
		}

//		String[] nos = null;
//
//		 switch (type) {
//		 case 'S':
//		 nos = getAllNo(cp.getYearNo(), count, cp.getSmaxNo(), cp.getSno());
//		 cp.setSmaxNo(cp.getSmaxNo() + count);
//		 break;
//		 case 'Q':
//		 nos = getAllNo(cp.getYearNo(), count, cp.getQmaxNo(), cp.getQno());
//		 cp.setSmaxNo(cp.getQmaxNo() + count);
//		 break;
//		 case 'J':
//		 nos = getAllNo(cp.getYearNo(), count, cp.getJmaxNo(), cp.getJno());
//		 cp.setSmaxNo(cp.getJmaxNo() + count);
//		 break;
//		 case 'F':
//		 nos = getAllNo(cp.getYearNo(), count, cp.getFmaxNo(), cp.getFno());
//		 cp.setSmaxNo(cp.getFmaxNo() + count);
//		 break;
//		 case 'T':
//		 nos = getAllNo(cp.getYearNo(), count, cp.getTmaxNo(), cp.getTno());
//		 cp.setSmaxNo(cp.getTmaxNo() + count);
//		 break;
//		 default:
//		 break;
//		 }
//		
//		 this.currentPeriodDao.update(cp);
//		 return nos;

		String yearNo = cp.getYearNo();
		String typeStr = "";
		Integer maxNo = 0;
		switch (type) {
		case 'S':
			typeStr = cp.getSno();
			maxNo = cp.getSmaxNo();
			cp.setSmaxNo(cp.getSmaxNo() + count);
			break;
		case 'Q':
			typeStr = cp.getQno();
			maxNo = cp.getQmaxNo();
			cp.setQmaxNo(cp.getQmaxNo() + count);
			break;
		case 'J':
			typeStr = cp.getJno();
			maxNo = cp.getJmaxNo();
			cp.setJmaxNo(cp.getJmaxNo() + count);
			break;
		case 'F':
			typeStr = cp.getFno();
			maxNo = cp.getFmaxNo();
			cp.setFmaxNo(cp.getFmaxNo() + count);
			break;
		case 'T':
			typeStr = cp.getTno();
			maxNo = cp.getTmaxNo();
			cp.setTmaxNo(cp.getTmaxNo() + count);
			break;
		default:
			break;
		}
		
		this.currentPeriodDao.update(cp);
		
		return this.getAllNo(yearNo, count, maxNo);
	}

	public void createCurr(String year) {
		CurrentPeriod currentPeriod = new CurrentPeriod();
		currentPeriod.setYearNo(year);
		currentPeriod.setStartTime(new DoDate().getStrToDate(year+"-01-01"));
		currentPeriod.setEndTime(new DoDate().getStrToDate(year+"-12-31"));
		currentPeriod.setStartNo("5");
		currentPeriod.setSerialBit("4");
		currentPeriod.setSno("S");
		currentPeriod.setQno("Q");
		currentPeriod.setJno("J");
		currentPeriod.setFno("F");
		currentPeriod.setTno("T");
		currentPeriod.setSmaxNo(0);
		currentPeriod.setQmaxNo(0);
		currentPeriod.setJmaxNo(0);
		currentPeriod.setFmaxNo(0);
		currentPeriod.setTmaxNo(0);

		try {
			// System.out.println("create");
			this.currentPeriodDao.save(currentPeriod);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 判断是否为建账阶段
	 * 
	 * @return true 建账 false 维护账
	 */
	public Boolean isCreate() {
		GFunc func = new GFunc();
		func.setFuncName("accountStatus");
		GFunc f = null;
		try {
			f = this.gfuncDao.findOneByExample(func);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return f == null ? false : true;
	}

	/**
	 * 创建本年度单据流水号
	 */
	public void createBillNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yy");
		String currYear = sdf.format(new Date());
		Danjuno d = new Danjuno();
		d.setFDanJuNo(0);
		d.setJDanJuNo(0);
		d.setPrefix(currYear);
		d.setQDanJuNo(0);
		d.setSDanJuNo(0);
		d.setTDanJuNo(0);
		d.setFBDanJuNo(0);
		d.setJBDanJuNo(0);
		d.setQBDanJuNo(0);
		d.setSBDanJuNo(0);
		d.setTBDanJuNo(0);
		try {
			danjunoDao.save(d);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getBillNo(char type) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yy");
			String currYear = sdf.format(new Date());
			Danjuno exampleDanjuno = new Danjuno();
			exampleDanjuno.setPrefix(currYear);
			Danjuno d = this.danjunoDao.findOneByExample(exampleDanjuno);
			if (d == null) {
				createBillNo();
			}
			d = this.danjunoDao.findOneByExample(exampleDanjuno);

			Integer billNo = 0;

			switch (type) {
			case 'S':
				billNo = d.getSDanJuNo();
				billNo += 1;
				d.setSDanJuNo(billNo);
				break;
			case 'Q':
				billNo = d.getQDanJuNo();
				billNo += 1;
				d.setQDanJuNo(billNo);
				break;
			case 'J':
				billNo = d.getJDanJuNo();
				billNo += 1;
				d.setJDanJuNo(billNo);
				break;
			case 'F':
				billNo = d.getFDanJuNo();
				billNo += 1;
				d.setFDanJuNo(billNo);
				break;
			case 'T':
				billNo = d.getTDanJuNo();
				billNo += 1;
				d.setTDanJuNo(billNo);
				break;
			default:
				break;
			}

			danjunoDao.update(d);

			return d.getPrefix() + this.filler(billNo, 6);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("单据号生成错误");
			e.printStackTrace();
			return "";
		}

	}
	
	public String getChangeBillNo(char type) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yy");
			String currYear = sdf.format(new Date());
			Danjuno exampleDanjuno = new Danjuno();
			exampleDanjuno.setPrefix(currYear);
			Danjuno d = this.danjunoDao.findOneByExample(exampleDanjuno);
			if (d == null) {
				createBillNo();
			}
			d = this.danjunoDao.findOneByExample(exampleDanjuno);

			Integer billNo = 0;

			switch (type) {
			case 'S':
				billNo = d.getSBDanJuNo();
				billNo += 1;
				d.setSBDanJuNo(billNo);
				break;
			case 'Q':
				billNo = d.getQBDanJuNo();
				billNo += 1;
				d.setQBDanJuNo(billNo);
				break;
			case 'J':
				billNo = d.getJBDanJuNo();
				billNo += 1;
				d.setJBDanJuNo(billNo);
				break;
			case 'F':
				billNo = d.getFBDanJuNo();
				billNo += 1;
				d.setFBDanJuNo(billNo);
				break;
			case 'T':
				billNo = d.getTBDanJuNo();
				billNo += 1;
				d.setTBDanJuNo(billNo);
				break;
			default:
				break;
			}

			danjunoDao.update(d);

			return d.getPrefix() + this.filler(billNo, 6);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("单据号生成错误");
			e.printStackTrace();
			return "";
		}

	} 

	public CurrentPeriodDAO getCurrentPeriodDao() {
		return currentPeriodDao;
	}

	public void setCurrentPeriodDao(CurrentPeriodDAO currentPeriodDao) {
		this.setBaseDao(currentPeriodDao);
		this.currentPeriodDao = currentPeriodDao;
	}

	public GFuncDAO getGfuncDao() {
		return gfuncDao;
	}

	public void setGfuncDao(GFuncDAO gfuncDao) {
		this.gfuncDao = gfuncDao;
	}

	public DanjunoDAO getDanjunoDao() {
		return danjunoDao;
	}

	public void setDanjunoDao(DanjunoDAO danjunoDao) {
		this.danjunoDao = danjunoDao;
	}

	

}
